import fetch from '../../utils/fetch';
import {gotoLogin} from '../../utils/util';
const globalData = getApp().globalData;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    navItem:0, //导航栏选中id
    topBarData:[
      {
        id:0,
        name:'动态'
      },
      // {
      //   id:1,
      //   name:'活动'
      // },
      {
        id:2,
        name:'排行榜'
      }
    ],
    postType: 1, // 1代表动态，2代表活动
    topPosition:0,
    dynamicArr:[  //动态数据
      {
        id:1,
        content:'今天完成跑步任务五公里，下次继续努力，冲冲冲',
        imgs:['https://picnew10.photophoto.cn/20160715/zhengzaipaobudenanrentupian-27069651_1.jpg'],
        createdAt: '1677046922662',
        place: '广东财经大学',
        likeNum: 45,
        commentNum:23,
        likeStatus:true,
        user:{
          id:5,
          username:'豆豆',
          avatar:'../../assets/images/baobao.jpg'
        }
      },
    ],
    dynamicPage:1, //动态页码
    dynamicCount:0, //动态文章数量
    todayDate:'',  //今天日期
    teamTriggered:false,  //自定义下拉刷新状态 
    rankDataArr: [{
      title:'积分排行榜(前十)',
      content: {}
    },
    {
      title: '同校积分排行榜(前十)',
      content: {}
    }
  ], // 排行榜
  },
  //每次获取文章条数
  size:5,
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getRank();
    //获取动态文章
    this.size = 5;
    this.getPostList(this.size,this.data.dynamicPage).then(res=>{
      const {count,postArr} = res.data;
      this.setData({
        dynamicArr:postArr,
        dynamicCount:count,
      });
    });
  },

  changeItem(e){
    const idx = e.detail.index;
    this.setData({
      navItem:idx
    });
  },
  changeTab(e){
    const idx = e.detail.current;
    this.setData({
      navItem:idx,
    });
  },
   // 回到顶部
   gotoTop:function(){
    this.setData({
      topPosition:0,
    });
  },
  gotoPublish(){
    if(!globalData.isLogin){
      loginGo();
      return;
    }
    wx.navigateTo({
      url: '../postPublish/postPublish',
    })
  },
  // 获取列表
  getPostList(size,page){
    return fetch('/post/list',{
      size,page,userId:globalData.userInfo.id?globalData.userInfo.id:'',
    })
  },
  getMoreArticle(e){
    const postType = e.currentTarget.dataset.postType;
    if(postType ==1){
      //获取更多动态文章
      if(this.data.dynamicPage*this.size > this.data.dynamicCount){
        return;
      }
      this.data.dynamicPage++;
      this.getPostList(this.size,this.data.dynamicPage).then(res=>{
        let dynamicArr = this.data.dynamicArr;
        res.data.postArr.forEach(item=>{
          dynamicArr.push(item);
        });
        this.setData({
          dynamicArr,
        })
      })
    }
  },
  onRefresh(e){
    const postType = e.currentTarget.dataset.postType;
    var that = this;
    wx.showLoading({
      title: '刷新中',
    });
    if(postType == 1){
      this.data.dynamicPage = 1;
      this.data.dynamicCount = 0;
      this.getPostList(this.size,this.data.dynamicPage).then(res=>{
        const {count,postArr} = res.data;
        this.setData({
          dynamicArr:postArr,
          dynamicCount:count,
          triggered: false
        });
        wx.showToast({
          title: '刷新成功',
        })
      }).catch(err=>{
        this.onRefresh()
      })
    }
  },
  gotoPostDetail(e){
    const postId = e.currentTarget.dataset.postId;
    wx.navigateTo({
      url: '../postDetail/postDetail?postId=' + postId,
    });
  },
  // 排行榜
  getRank(){
    fetch(`/users/rank?college=${globalData.userInfo.college}`,{
    }).then(res=>{
      console.log(res);
      this.setData({
        'rankDataArr[0].content':res.data.scoreRank,
        'rankDataArr[1].content':res.data.scoreCollegeRank
      });
    }).catch(err=>{
      this.getRank();
    })
  },
   // 获取我的排行
   getMyRank:async function(e){
    if(!globalData.isLogin){
      gotoLogin();
      return;
    }
    // 要展示的数据
    let showData = {};
    const idx = e.currentTarget.dataset.idx;
    if(idx == 0){
      await fetch(`/users/myRank?score=${globalData.userInfo.score}`).then(res=>{
        showData = res.data.myRank;
      })
    }else{
      await fetch(`/users/myRankCollege?score=${globalData.userInfo.score}&college=${globalData.userInfo.college}`).then(res=>{
        showData = res.data.myRank;
      })
    };
    wx.showModal({
      title: '我的排名',
      content: `本人排名第${showData.rankNum}名`,
      showCancel:false,
      confirmText:'已知晓'
    })
  },
})